package com.loong.game2048.dao;

import com.loong.game2048.module.Forum;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ForumDao {

    @Insert("insert into forum (u_id,title,tag,content,time) values(#{uid},#{title},#{tag},#{content},#{time})")
    @SelectKey(statement = "select last_insert_id()" ,keyProperty = "id",keyColumn = "id",resultType = int.class,before = false)
    Integer addForum(Forum forum);

    @Select("select count(*) from forum")
    Integer countAll();

    @Select("select * from forum order by time desc limit #{offset} , #{size} ")
    @Results(id = "forum",value = {
            @Result(property = "uid",column = "u_id")
    })
    List<Forum> list(@Param("offset") int offset, @Param("size") int size);

    @Select("select * from forum where id = #{id}")
    @ResultMap("forum")
    Forum findById(@Param("id") int id);

    @Delete("DELETE FROM forum WHERE id = #{id}")
    void delete(@Param("id") int id);

    @Select("select count(*) from forum where tag = #{tag}")
    Integer countAllwithTag(Integer tag);

    @Select("select * from forum where tag = #{tag} order by time desc limit #{offset} , #{size} ")
    @ResultMap("forum")
    List<Forum> listWithTag(@Param("offset") int offset, @Param("size") int size,@Param("tag") int tag);

    @Update("update forum set title = #{title} , tag = #{tag} , content = #{content}  where id = #{id}")
    void update(Forum forum);
}
